package com.cn.bopu.common.upms.commmon.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "公共响应vo")
public class CommonResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    // 成功标志
    public static final int SUCCESS= 0;
    public static final int FAIL= -1;
    public static final int BUSY= -100;
    // 响应提示语
    public static final String SUCCESS_TEXT = "Success";
    public static final String FAIL_TEXT = "Fail";
    public static final String BUSY_TEXT = "Busy";

    @ApiModelProperty(value = "成功标志：0成功，-1失败")
    private Integer status;
    @ApiModelProperty(value = "响应提示语")
    private String statusText;
    @ApiModelProperty(value = "响应数据")
    private T data;

    /**
     * 默认请求成功
     */
    public CommonResult() {
        this.status = SUCCESS;
        this.statusText = SUCCESS_TEXT;
    }

    /**
     * 有参构造
     *
     * @param status 成功标志
     * @param statusText 响应提示语
     * @param data 响应数据
     */
    public CommonResult(Integer status, String statusText, T data) {
        this.status = status;
        this.statusText = statusText;
        this.data = data;
    }

    /**
     * 有参构造 自动判断data类型是否有效
     *      有效：返回成功数据
     *      无效：返回异常信息
     *
     * @param data 响应数据
     */
    public CommonResult(T data) {
        if (data instanceof Exception) {
            this.status = FAIL;
            this.statusText = ((Exception) data).getLocalizedMessage();
        } else {
            this.status = SUCCESS;
            this.statusText = SUCCESS_TEXT;
            this.data = data;
        }
    }
}
